#include "mainwindow.h"
#include "./ui_mainwindow.h"

#include <QtWidgets>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),generationCount(0)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 连接信号和槽
    connect(ui->startButton, &QPushButton::clicked, this, &MainWindow::startGame);
    connect(ui->pauseButton, &QPushButton::clicked, this, &MainWindow::pauseGame);
    connect(ui->resetButton, &QPushButton::clicked, this, &MainWindow::resetGame);

    // 按钮启用状态
    ui->pauseButton->setEnabled(false);

    cells = std::vector<std::vector<bool>>(rows, std::vector<bool>(cols, false));

    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &MainWindow::nextGeneration);

    resetGame();
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::paintEvent(QPaintEvent * /*event*/)
{
    QPainter painter(this);
    // 设置背景颜色
    painter.fillRect(rect(), QColor(255, 255, 255)); // 白色背景

    // 画网格线
    QPen gridPen(Qt::lightGray, 1, Qt::DotLine); // 第二个参数是线宽，第三个参数是线型
    painter.setPen(gridPen);
    for (int i = 0; i <= rows; ++i) {
        painter.drawLine(0, i * cellSize, cols * cellSize, i * cellSize);
    }
    for (int j = 0; j <= cols; ++j) {
        painter.drawLine(j * cellSize, 0, j * cellSize, rows * cellSize);
    }

    // 画细胞
    painter.setPen(Qt::NoPen);
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            if (cells[i][j]) {
                painter.fillRect(j * cellSize, i * cellSize, cellSize, cellSize, Qt::darkGreen);
            } else {
                painter.fillRect(j * cellSize, i * cellSize, cellSize, cellSize, QColor(0, 0, 0));
            }
        }
    }

    // 绘制边框
    painter.setPen(QPen(Qt::black, 2));
    painter.drawRect(0, 0, cols * cellSize, rows * cellSize);
}


void MainWindow::nextGeneration()
{
    std::vector<std::vector<bool>> nextGen(rows, std::vector<bool>(cols, false));

    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            int liveNeighbors = countNeighbors(i, j);
            if ((cells[i][j] && (liveNeighbors == 2 || liveNeighbors == 3)) || (!cells[i][j] && liveNeighbors == 3)) {
                nextGen[i][j] = true;
            }
        }
    }

    cells.swap(nextGen);
    ++generationCount;
    ui->generationLabel->setText(QString("生成: %1").arg(generationCount));
    update();
}

int MainWindow::countNeighbors(int row, int col) const
{
    int count = 0;
    for (int i = -1; i <= 1; ++i) {
        for (int j = -1; j <= 1; ++j) {
            if (i == 0 && j == 0) continue;
            int r = (row + i + rows) % rows;
            int c = (col + j + cols) % cols;
            count += cells[r][c];
        }
    }
    return count;
}

void MainWindow::startGame()
{
    timer->start(100);
    ui->startButton->setEnabled(false);
    ui->pauseButton->setEnabled(true);
}

void MainWindow::pauseGame()
{
    timer->stop();
    ui->startButton->setEnabled(true);
    ui->pauseButton->setEnabled(false);
}

void MainWindow::resetGame()
{
    cells.assign(rows, std::vector<bool>(cols, false));

    update();
    ui->pauseButton->setEnabled(false);
    ui->startButton->setEnabled(true);
}

void MainWindow::setCellSize(int newSize)
{
    cellSize = newSize;
}


void MainWindow::mousePressEvent(QMouseEvent *event)
{
    toggleCell(event->pos());
    isDragging = true; // 添加一个标志表示开始拖动
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if (isDragging)
    {
        toggleCell(event->pos());
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    isDragging = false; // 结束拖动
}

void MainWindow::toggleCell(const QPoint &point)
{
    int x = point.x() / cellSize;
    int y = point.y() / cellSize;
    if (x >= 0 && x < cols && y >= 0 && y < rows) {
        cells[y][x] = !cells[y][x];
        update();
    }
}
